package xyz.o0o0o.demo.SmackDemo;

import java.io.IOException;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws XMPPException, SmackException, IOException, InterruptedException {
		System.out.println("Hello World!");

		XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
		configBuilder.setServiceName("127.0.0.1");
		configBuilder.setHost("127.0.0.1");
		configBuilder.setPort(5222);
		configBuilder.setSecurityMode(SecurityMode.disabled);
		//configBuilder.setDebuggerEnabled(true);

		AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
		System.out.println("connecting");
		AbstractXMPPConnection connect = connection.connect();
		System.out.println("connected");
		connect.login("admin", "admin");

		ChatManager chatManager = ChatManager.getInstanceFor(connection);
		Chat chat = chatManager.createChat("bob@wbh");
		
		chatManager.addChatListener(new ChatManagerListener() {
			
			public void chatCreated(Chat chat, boolean createdLocally) {
				if (!createdLocally)
					chat.addMessageListener(new ChatMessageListener() {
						
						public void processMessage(Chat chat, Message message) {
							System.out.println("Received message: " + message);
						}
					} );
			}
		});
		Message newMessage = new Message();
		newMessage.setBody("Howdy!222");
		chat.sendMessage(newMessage);

		Thread.currentThread().sleep(50000);
		connection.disconnect();
	}
}
